package com.newsproject.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import com.newsproject.mapper.SearchInUserPageMapper;
import com.newsproject.pojo.Post;
import com.newsproject.service.SearchInUserPageService;

@Service
public class SearchInUserPageServiceImpl implements SearchInUserPageService {

    @Autowired
    private SearchInUserPageMapper searchInUserPageMapper;
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public ArrayList<Post> searchMyNews(int user_id, String title, String start_time, String end_time, int type,
            int status,int startIndex,int pageSize) {
        return searchInUserPageMapper.searchMyNews(user_id, title, start_time, end_time, type, status,startIndex,pageSize);
    }

    @Override
    public ArrayList<Post> searchMyBookmarks(int user_id, String title, String start_time, String end_time,
            int status,int startIndex,int pageSize) {
        return searchInUserPageMapper.searchMyBookmarks(user_id, title, start_time, end_time, status,startIndex,pageSize);
    }

    @Override
    public boolean checkCategoryIdOrUserId(int category_id, int user_id) {
        // 检查用户ID是否存在
        if (searchInUserPageMapper.checkUser(user_id) == null) {
            return false; // 用户ID不存在
        }
        // 检查分类ID是否存在
        if (searchInUserPageMapper.checkCategory(category_id) == null) {
            return false; // 分类ID不存在
        }
        return true; // 用户ID和分类ID都存在
    }

    @Override
    public boolean checkTimeRange(String start_time, String end_time) {
        // 在时间都不为空时，检查时间范围是否合法
        if (start_time == null || start_time.equals("") || end_time == null || end_time.equals("")) {
            return true; // 时间范围为空，合法
        }
        try {
            Date start = dateFormat.parse(start_time);
            Date end = dateFormat.parse(end_time);
            if (start.after(end)) {
                return false; // 起始时间大于结束时间，不合法
            }
        } catch (ParseException e) {
            return false; // 时间格式不正确，不合法
        }
        return true; // 时间范围合法
    }

    @Transactional
    @Override
    public ModelAndView search(int user_id, String title, String start_time, String end_time, Integer category_id,
            Integer audit_status) {
        ModelAndView response = new ModelAndView("personal_page");

        // 处理搜索字符串，去除多余空格
        if(title == null) title = "";
        title = title.trim();
        // 日期格式验证
        try {
            if (start_time != null && !start_time.isEmpty()) {
                dateFormat.parse(start_time);
            }
            if (end_time != null && !end_time.isEmpty()) {
                dateFormat.parse(end_time);
            }
        } catch (Exception e) {
            response.addObject("message", "时间格式错误！");
            return response;
        }

        if(audit_status == null) audit_status = 1;
        // 检测分类ID和用户ID是否存在
        if(category_id != null) {
            if (checkCategoryIdOrUserId(category_id, user_id) == false) {
                response.addObject("message", "用户ID或分类ID不存在！");
                return response;
            }
        } else category_id = -1;
        // 检测时间范围是否合法
        if (checkTimeRange(start_time, end_time) == false) {
            response.addObject("message", "时间范围不合法！");
            return response;
        }
        // 检测审核状态是否合法
        if (audit_status < 0 || audit_status > 2) {
            response.addObject("message", "审核状态不合法！");
            return response;
        }

        // 调用业务逻辑层方法，获取搜索结果
        ArrayList<Post> postList = searchMyNews(user_id, title, start_time, end_time, category_id, audit_status,0,10);
        if (postList == null) {
            response.addObject("message", "没有找到相关的新闻！");
            return response;
        }
        response.addObject("postList", postList);
        response.addObject("message", "搜索成功！");

        return response;
    }

    @Transactional
    @Override
    public Map<String, Object> Search(int user_id, String title, String start_time, String end_time, Integer category_id,
            Integer audit_status,int page) {
        Map<String, Object> response = new HashMap<>();

        // 处理搜索字符串，去除多余空格
        if(title == null) title = "";
        title = title.trim();
        // 日期格式验证
        try {
            if (start_time != null && !start_time.isEmpty()) {
                dateFormat.parse(start_time);
            }
            if (end_time != null && !end_time.isEmpty()) {
                dateFormat.parse(end_time);
            }
        } catch (Exception e) {
            response.put("message", "时间格式错误！");
            return response;
        }

        if(audit_status == null) audit_status = 1;
        // 检测分类ID和用户ID是否存在
        if(category_id != null) {
            if (checkCategoryIdOrUserId(category_id, user_id) == false) {
                response.put("message", "用户ID或分类ID不存在！");
                return response;
            }
        } else category_id = -1;
        // 检测时间范围是否合法
        if (checkTimeRange(start_time, end_time) == false) {
            response.put("message", "时间范围不合法！");
            return response;
        }
        // 检测审核状态是否合法
        if (audit_status < 0 || audit_status > 2) {
            response.put("message", "审核状态不合法！");
            return response;
        }
        // 检查页数是否合法
        if (page < 1) {
            response.put("message", "页数不合法！");
            return response;
        }
        int startIndex = (page-1)*10;

        // 调用业务逻辑层方法，获取搜索结果
        ArrayList<Post> postList = searchMyNews(user_id, title, start_time, end_time, category_id, audit_status,startIndex,10);
        ArrayList<Post> nextPage = searchMyNews(user_id, title, start_time, end_time, category_id, audit_status,startIndex+10,1);
        if (postList == null || postList.size() == 0) {
            response.put("message", "没有找到相关的新闻！");
            return response;
        }
        if (nextPage == null || nextPage.size() == 0 ) {
            response.put("hasMore", false); // 没有下一页
        } else {
            response.put("hasMore", true); // 有下一页
        }
        response.put("postList", postList);
        response.put("message", "搜索成功！");

        return response;
    }

    @Transactional
    @Override
    public Map<String, Object> Search_bookmark(int user_id, String title, String start_time, String end_time,
            Integer audit_status,int page) {
        Map<String, Object> response = new HashMap<>();

        // 处理搜索字符串，去除多余空格
        if(title == null) title = "";
        title = title.trim();
        // 日期格式验证
        try {
            if (start_time != null && !start_time.isEmpty()) {
                dateFormat.parse(start_time);
            }
            if (end_time != null && !end_time.isEmpty()) {
                dateFormat.parse(end_time);
            }
        } catch (Exception e) {
            response.put("message", "时间格式错误！");
            return response;
        }

        if(audit_status == null) audit_status = 1;
        // 检测时间范围是否合法
        if (checkTimeRange(start_time, end_time) == false) {
            response.put("message", "时间范围不合法！");
            return response;
        }
        // 检测审核状态是否合法
        if (audit_status < 0 || audit_status > 2) {
            response.put("message", "审核状态不合法！");
            return response;
        }
        // 检查页数是否合法
        if (page < 1) {
            response.put("message", "页数不合法！");
            return response;
        }
        int startIndex = (page-1)*10;

        // 调用业务逻辑层方法，获取搜索结果
        ArrayList<Post> postList = searchMyBookmarks(user_id, title, start_time, end_time, audit_status,startIndex,10);
        ArrayList<Post> nextPage = searchMyBookmarks(user_id, title, start_time, end_time, audit_status,startIndex+10,1);
        if (postList == null || postList.size() == 0) {
            response.put("message", "没有找到相关的新闻！");
            return response;
        }
        if (nextPage == null || nextPage.size() == 0 ) {
            response.put("hasMore", false); // 没有下一页
        } else {
            response.put("hasMore", true); // 有下一页
        }
        response.put("postList", postList);
        response.put("message", "搜索成功！");

        return response;
    }
}
